package com.csproj.basic.resource;

import com.codahale.metrics.annotation.Timed;
import com.csproj.basic.service.ElectronicLockService;
import com.csproj.basic.service.dto.ElectronicLockDTO;
import com.csproj.siiat.base.common.dto.BaseDTO;
import com.csproj.siiat.base.common.repository.PageableTools;
import com.csproj.system.domain.UserInfo;
import com.csproj.system.service.SysLogsService;
import com.csproj.system.service.UserInfoService;
import net.logstash.logback.encoder.org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * REST controller for managing ElectronicLock.
 */
@RestController
@RequestMapping("/api/siiat/v1")
public class ElectronicLockResource {

    private final Logger log = LoggerFactory.getLogger(ElectronicLockResource.class);

    private static final String ENTITY_NAME = "electronicLock";

    private final ElectronicLockService electronicLockService;
    private final UserInfoService userInfoService;
    private final SysLogsService sysLogsService;
    public ElectronicLockResource(ElectronicLockService electronicLockService,UserInfoService userInfoService,SysLogsService sysLogsService) {
        this.electronicLockService = electronicLockService;
        this.sysLogsService = sysLogsService;
        this.userInfoService = userInfoService;
    }
    /**
     * POST  /electroniclock : 创建电子关锁
     * @param dto dto
     * @return
     * TODO 保存post方法
     * TODO 每个方面里面都要加try catch处理
     */
    @PostMapping("/electroniclock")
    @Timed
    public BaseDTO createElectronicLock(@Valid @RequestBody ElectronicLockDTO dto){
        ElectronicLockDTO result = electronicLockService.saveAndGetDTO(dto);
        UserInfo userInfo = userInfoService.getCurrentUser();
        sysLogsService.saveForGet("", userInfo.getName()+":user_create_electironic","");
        return BaseDTO.createBaseDTO().setData(result);
    }

    /**
     * PUT  /electroniclock : 修改电子关锁.
     * @param dto  dto
     * @return
     */
    @PutMapping("/electroniclock/{id}")
    @Timed
    public BaseDTO updateElectronicLock(@Valid @RequestBody ElectronicLockDTO dto, @PathVariable String id) throws URISyntaxException {
        dto.setId(id);
        ElectronicLockDTO result = electronicLockService.updateAndGetDTO(dto);
        UserInfo userInfo = userInfoService.getCurrentUser();
        sysLogsService.saveForGet("", userInfo.getName()+":user_update_electironic","");
        return BaseDTO.createBaseDTO().setData(result);
    }

    /**
     * GET  /electroniclock : 获取电子关锁
     * @return 电子关锁
     */
    @GetMapping("/electroniclock")
    @Timed
    public BaseDTO getPageElectronicLocks(@RequestParam(value = "pageNumber",required = false) Integer pageNum,
                                          @RequestParam(value = "pageSize",required = false) Integer pageSize,
                                          @RequestParam(value = "lockNo",required = false) String lockNo,
                                          @RequestParam(value = "siteId",required = false) String siteId,
                                          @RequestParam(value = "state",required = false) List<String> states) {
        Pageable pageable = PageableTools.basicPage(pageNum,pageSize);
        Map<String,Object> map = new HashMap<>();
        map.put("lockNo",lockNo);
        map.put("siteId",siteId);
        map.put("states",states);
        BaseDTO result = electronicLockService.findPageListAndGetDTO(map,pageable);
        UserInfo userInfo = userInfoService.getCurrentUser();
        sysLogsService.saveForGet("", userInfo.getName()+":user_view_electironic","");
        return result;
    }

    /**
     * GET  /electroniclocksite : 获取电子关锁
     * @return 电子关锁
     */
    @GetMapping("/electroniclocksite")
    @Timed
    public BaseDTO getListElectronicLockAndMessage(@RequestParam(value = "siteId",required = false) String siteId)
    {
        return BaseDTO.createBaseDTO().setFieldList(electronicLockService.findAllAndMessage(siteId));
    }

    /**
     * GET  /electroniclock : 获取电子关锁信息(不分页)
     * @return BaseDTO
     */
    @GetMapping("/allelectroniclock")
    @Timed
    public BaseDTO getAllElectronicLocks() {
        return BaseDTO.createBaseDTO().setFieldList(electronicLockService.findAll());
    }

    /**
     * GET  /electroniclock/:id : 根据设备id，获取读取设备信息
     *
     * @param id id
     * @return pda信息
     */
    @GetMapping("/electroniclock/{id}")
    @Timed
    public BaseDTO getElectronicLock(@PathVariable String id) {
        ElectronicLockDTO dto = electronicLockService.findOneAndGetDTO(id);
        return BaseDTO.createBaseDTO().setData(dto);
    }

    /**
     * DELETE  /electroniclock/:id : 根据id，删除读取设备信息.
     * @param id id
     * @return
     */
    @DeleteMapping("/electroniclock/{id}")
    @Timed
    public BaseDTO deleteElectronicLock(@PathVariable String id) {
        electronicLockService.delete(id);
        return BaseDTO.createBaseDTO().setMessage("delete_success");
    }

    /**
     * PUT  /electroniclock/updateElectronicStatus/{id} : 修改电子关锁使用状态
     *
     * @param electronicLockDTO
     * @param id
     * @return BaseDTO
     * @throws URISyntaxException
     */
    @PutMapping("/electroniclock/updateElectronicStatus/{id}")
    @Timed
    public BaseDTO updateElectronicStatus(HttpServletRequest request, @RequestBody(required = false) ElectronicLockDTO electronicLockDTO, @PathVariable String id) throws URISyntaxException {
        log.debug("REST request to update UserInfo isUse : {}", electronicLockDTO);
        electronicLockDTO.setId(id);
        ElectronicLockDTO result = electronicLockService.updateElectronicStatus(electronicLockDTO);

        UserInfo userInfo = userInfoService.getCurrentUser();
        sysLogsService.saveForGet("", userInfo.getName()+":user_update_electironic_status","");
        return BaseDTO.createBaseDTO(result).setMessage("user_update_electironic_status_success");
    }
    /**
     * PUT  /electroniclock/updateEnable/{id} : 修改电子关锁使用状态
     *
     * @param dto
     * @param id
     * @return BaseDTO
     * @throws URISyntaxException
     */
    @PutMapping("/electroniclock/updateEnable/{id}")
    @Timed
    public BaseDTO updateElectronicLockEnable(HttpServletRequest request, @RequestBody(required = false) ElectronicLockDTO dto, @PathVariable String id) throws URISyntaxException {
        dto.setId(id);
        if (dto.getId() == null) {
            return createElectronicLock(dto);
        }
        ElectronicLockDTO result = electronicLockService.status(dto.getId(),dto.getEnableFlag());

        UserInfo userInfo = userInfoService.getCurrentUser();
        sysLogsService.saveForGet("", userInfo.getName()+":user_update_electironic_status","");
        return BaseDTO.createBaseDTO(result).setMessage("user_update_electironic_status_success");
    }

    /**
     *电子关锁统计
     */
    @GetMapping("/electroniclock/statistics")
    @Timed
    public BaseDTO findElectronicLockStatistics (@RequestParam(value = "siteId",required = false) String siteId) {
        Map<String,Object> result = electronicLockService.findElectronicLockStatistics(siteId);
        return BaseDTO.createBaseDTO(result);
    }

    /**
     *  批量导入
     */
    @PostMapping("/electroniclock/batchImport")
    @Timed
    public BaseDTO eleBatchImport(MultipartFile file){
        UserInfo userInfo = userInfoService.getCurrentUser();
        sysLogsService.saveForGet("", userInfo.getName()+":user_update_electironic_batch_import","");
        return electronicLockService.batchImport(file) ;
    }
}
